package com.athena.controller
{
	import com.greensock.events.LoaderEvent;
	import com.greensock.loading.ImageLoader;
	import com.greensock.loading.LoaderMax;
	import com.greensock.loading.SWFLoader;
	import com.junkbyte.console.Cc;
	import flash.display.MovieClip;
	import flash.events.DataEvent;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.utils.getDefinitionByName;
	
	/**
	 * ...
	 * @author chau
	 */
	public class LoaderFile extends EventDispatcher
	{
		private var _configData:XML;
		public static var LOADCONFIG:String = "load_config";
		public static var LOAD_ASSET_PROCESS:String = "load_asset_process";
		public static var LOAD_ASSET_COMPLETE:String = "load_asset_complete";
		
		private var _dash  : MovieClip;
		private var _loadAssetCompl:Boolean = false;
		
		private var _queue:LoaderMax;
		public function LoaderFile() {
		
		}
		
		public function loadConfig():void {
			var req:URLLoader = new URLLoader();
			req.load(new URLRequest("http://localhost/game68/config.xml?cache=" + new Date().getTime()));
			req.addEventListener(Event.COMPLETE, loadComplete);
			req.addEventListener(IOErrorEvent.IO_ERROR, loadError);
			
			function loadComplete(e:Event):void {
				_configData = new XML(req.data);
				dispatchEvent(new Event(LOADCONFIG));
				
				req.removeEventListener(Event.COMPLETE, loadComplete);
				req.removeEventListener(IOErrorEvent.IO_ERROR, loadError);
			}
			function loadError(e:IOErrorEvent):void {
				Cc.log("Error reading the data file. The server may be busy. Try refreshing the page.");
			}
		}
		
		public function loadAssetFile() : void {
			_queue = new LoaderMax({maxConnections:1, 
									onProgress:queueProgressHandler,
									onComplete:queueCompleteHandler,
									onChildProgress:childProgressHandler, 
									onChildComplete:childCompleteHandler
									});
			_queue.append(new SWFLoader(Main.getInstance().config.dashboard.file + (new Date()).time, { name : Main.getInstance().config.dashboard.name, 
						estimatedBytes : Main.getInstance().config.dashboard.estimate, visible : false, autoPlay : false } ));
			_queue.load();
			function queueProgressHandler(event:LoaderEvent):void {
				dispatchEvent(new DataEvent(LOAD_ASSET_PROCESS, true, false, event.target.progress + ""));
			}
			function queueCompleteHandler(event:LoaderEvent):void {
				_dash = MovieClip((_queue.getLoader(Main.getInstance().config.dashboard.name) as SWFLoader).rawContent);
				dispatchEvent(new Event(LOAD_ASSET_COMPLETE));
			}
			
			function childProgressHandler(evt:LoaderEvent):void {
			}
		
			function childCompleteHandler(event:LoaderEvent):void {
			}	
		}
		public function get configData():XML {
			return _configData;
		}
		
		public function get loadAssetCompl():Boolean {
			return _loadAssetCompl;
		}
		
		public function get dash():MovieClip 
		{
			return _dash;
		}
		
		public function get queue():LoaderMax 
		{
			return _queue;
		}
	}

}